package com.iwhalecloud.bss.kite.cucc.order.accept.constructor.inst;

import com.iwhalecloud.bss.kite.common.constant.ServiceOfferConsts;
import com.iwhalecloud.bss.kite.common.constant.TableConsts;
import com.iwhalecloud.bss.kite.common.util.KiteBeanUtils;
import com.iwhalecloud.bss.kite.cucc.order.util.AcceptUtils;
import com.iwhalecloud.bss.kite.dataservice.entity.inst.ProdInst;
import com.iwhalecloud.bss.kite.dataservice.entity.inst.ProdInstState;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.enums.ActionType;
import com.iwhalecloud.bss.kite.manager.enums.InstType;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInstState;
import com.iwhalecloud.bss.kite.order.client.dto.InstContainer;
import com.iwhalecloud.bss.kite.order.client.dto.OrderRequest;
import com.ztesoft.bss.base.staticdata.CoreDataPublic;
import com.ztesoft.bss.common.util.SpringUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

/**
 * 产品实例停机记录构造器
 * @version 1.0
 */
@Component
public class ProdInstStateConstructor extends InstConstructor<KiteProdInstState, ProdInstState> {

    @Override
    public String getTableName() {
        return TableConsts.ProdInstState.getTableName();
    }

    @Override
    public InstType getParentInstType() {
        return InstType.PROD_INST;
    }

    @Override
    public ProdInstState build(OrderRequest orderRequest, String prodInstStateId, Map<String, Object> params) {
        if (Objects.isNull(orderRequest) || StringUtils.isBlank(prodInstStateId)) {
            return null;
        }

        IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);
        KiteProdInstState kiteProdInstState = querySceneInstService.getProdInstState(orderRequest.getCustId(),
            prodInstStateId);
        KiteProdInst kiteProdInst = querySceneInstService.getProdInstByProdInstStateId(orderRequest.getCustId(),
            prodInstStateId);
        if (isIneffectiveInst(kiteProdInstState) || isIneffectiveInst(kiteProdInst)) {
            return null;
        }

        params = Optional.ofNullable(params).orElse(new HashMap<>());
        params.put(KEY_PARENT_INST_TYPE, kiteProdInst.getInstType());
        params.put(KEY_PARENT_INST_ID, kiteProdInst.getProdInstId());

        kiteProdInstState.setProdInstId(kiteProdInst.getProdInstId());
        kiteProdInstState.setCompInstId(kiteProdInst.getOfferInstId());
        ProdInstState prodInstState = convert(orderRequest, kiteProdInstState, params);
        // 紧急开停机新增的prodInstState不需要入库
        if (!(Objects.equals(orderRequest.getServiceOfferId(), ServiceOfferConsts.SERVICE_OFFER_EMERGENCY_RECOVER)
            && AcceptUtils.isAddInst(prodInstState))) {
            InstContainer.ofRequest(orderRequest).addProdInstState(prodInstState);
        }

        ProdInst prodInst = buildParentInst(orderRequest, kiteProdInst.getProdInstId());
        if (AcceptUtils.isAddInst(prodInstState) && Objects.nonNull(prodInst)
            && StringUtils.isNotBlank(prodInst.getOwnerCustId())) {
            prodInstState.setOwnerCustId(prodInst.getOwnerCustId());
        }

        return prodInstState;
    }

    @Override
    public ProdInstState newInst(OrderRequest orderRequest, KiteProdInstState kiteProdInstState) {
        if (!ObjectUtils.allNotNull(orderRequest, kiteProdInstState)) {
            return null;
        }

        Date now = new Date();
        ProdInstState prodInstState = KiteBeanUtils.transform(kiteProdInstState, ProdInstState.class,
            KiteInst.ACTION_TYPE);
        prodInstState.setOwnerCustId(orderRequest.getCustId());
        prodInstState.setActionType(ActionType.A.name());
//        prodInstState.setEffDate(effDate);
//        prodInstState.setExpDate(expDate);
        prodInstState.setStatusDate(now);
        prodInstState.setCreateStaff(orderRequest.getStaffId());
        prodInstState.setLanId(orderRequest.getLanId());
        prodInstState.setCreateDate(now);
        prodInstState.setEventDate(now);
        prodInstState.setStatusCd(CoreDataPublic.PUB_C_0001_1200);
        prodInstState.setUpdateStaff(orderRequest.getStaffId());
        prodInstState.setUpdateDate(now);
        prodInstState.setLastOrderItemId("-1");

        return prodInstState;
    }
}
